package cn.whdreamblog.daoimp;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;

import cn.whdreamblog.dao.UserDAO;
import cn.whdreamblog.entites.User;
import cn.whdreamblog.util.MyDBUtil;

public class UserDAOImp extends BaseModelDAOImp implements UserDAO {
	
	public UserDAOImp() {
		super();
	}
	
	@Override
	public boolean updateUserName(User user) {
		return super.updateData(user);
	}

	@Override
	public boolean updatePwd(User user) {
		
		if(sessionFactory!=null){
			
			session=sessionFactory.openSession();
			try {
				tx=session.beginTransaction();
				session.update(user);
				tx.commit();
				return true;
			} catch (Exception e) {
				LOG.error("searchBlogArticleByAttr2Map:",e);
				MyDBUtil.rollBack(tx);
				
			}finally{
				MyDBUtil.closeSession(session, tx);
			}
		}
		return false;
	}

	@Override
	public boolean isExist(User user) {
		if(sessionFactory!=null){
			session=sessionFactory.openSession();
			try {
				tx=session.beginTransaction();
				List<User> list = session.createQuery("from User u where u.userName=?0 and u.pwd=?1")
						.setParameter("0", user.getUserName())
						.setParameter("1", user.getPwd())
						.list();
				tx.commit();
				if(list!=null&&list.size()>0){
					return true;
				}
				
			} catch (Exception e) {
				LOG.error("searchBlogArticleByAttr2Map:",e);
				e.printStackTrace();
				MyDBUtil.rollBack(tx);
				
			}finally{
				MyDBUtil.closeSession(session, tx);
			}
		}
		return false;
	}

}
